
/**
 * 
 * 
 * 
 */

const express = require('express')
//create router object
const r = express.Router()

//require pool
const conn = require('../pool.js')

//add router 

//1.uesr register api(post/reg)
//http://127.0.0.1:8888/v1/users/reg
/**
uname:bbb
upwd:123111
phone:15034088888
 */
r.post('/reg', (req, res, next) => {
  //1.1get the data form post request
  let obj = req.body
  console.log(obj)

  //1.2 judge whether the obj is empty?
  let arr = Object.keys(obj);
  console.log(arr)
  for (let i = 0; i < arr.length; i++) {
    if (obj[arr[i]] === '') {
      let str = arr[i];
      res.send(`${str} Cannot be empty`)
      return;
    }
  }

  //Check phone number
  // if (!/^1[3-9]\d{9}/.test(obj.phone)) {
  //   res.send({ code: 405, msg: 'phone number is wrong' })
  //   return;
  // }

  //Execute SQL command
  conn.query('insert into user set ?', [obj], (err, result) => {
    if (err) {
      //if wrong,give it to next middleware
      //not use throw err
      next(err);
      //Block execution
      return;
    };
    console.log(result)
    if (result.affectedRows === 0) {
      res.send('insert false')
    } else {
      res.redirect('/login.html');
      // res.send('insert ok')
    }
  })

  // res.send('register ok')
})

//2.user login(post/login)
//http://127.0.0.1:8888/v1/users/login
/**
  uname:aaa
  upwd:123111
 */
r.post('/login', (req, res, next) => {
  //get request body
  let obj = req.body
  //Execute SQL command
  conn.query(" select * from user where username = ? and userpassword = ? ", [obj.username, obj.userpassword], (err, result) => {
    if (err) {
      next(err);
      return;
    };
    console.log(result)
    if (result.length === 0) {
      res.send('login false')
    } else {
      res.redirect('/index.html');
      // res.send('login ok')
    }
  })
})

//3.user updata(put/updata)
//http://127.0.0.1:8888/v1/users/updata
/** uname:aaa
    upwd:123
    email:123
    phone:188888888
    uid:1
 */
r.put('/updata', (req, res, next) => {
  let obj = req.body
  //check empty?
  let arr = Object.keys(obj);
  console.log(arr)
  console.log(obj)
  for (let i = 0; i < arr.length; i++) {
    if (obj[arr[i]] === '') {
      let str = arr[i];
      res.send(`${str} Cannot be empty`)
      return;
    }
  }
  //Execute SQL command
  //UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
  conn.query("update xz_user set ? where uid = ?", [obj,obj.uid], (err, result) => {
    if (err) {
      next(err);
      return;
    };
    console.log(result)
    if (result.length === 0) {
      res.send('updata false')
    } else {
      res.send('updata ok')
    }
  })

})

//4.user getUser   (get  /getUser)
//http://127.0.0.1:8888/v1/users//getUser1
/**
uid:1
 */
r.get('/getUser:uid', (req, res, next) => {
  let obj = req.params
  console.log(obj)
  //Execute SQL command
  //UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
  conn.query("select * from user where uid = ?", [obj.uid], (err, result) => {
    if (err) {
      next(err);
      return;
    };
    console.log(result)
    if (result.affectedRows === 0) {
      res.send('/getUser false')
    } else {
      res.send('/getUser ok')
    }
  })

})

//Export router object
module.exports = r;








